import { isArray, isObject, isString, isFunction, keys, assign } from 'lodash';
import SelectArea from '/common/components/SelectArea';
import BusinessDataGrid from '/common/componentsTpl/BusinessDataGrid';
import DataGrid from '/common/componentsTpl/DataGrid';
import ComboBox from '/common/componentsTpl/ComboBox';
import TabNav from '/common/componentsTpl/TabNav';
import DocComp from '/common/componentsTpl/DocComp';
import Finder from '/common/componentsTpl/Finder';
import CheckBoxComp from '/common/componentsTpl/CheckBoxComp';
import StaticImg from '/common/componentsTpl/StaticImg';
import AcctItemResTarEditor from '/common/componentsTpl/AcctItemResTarEditor';
import AcctItemResItemEditor from '/common/componentsTpl/AcctItemResItemEditor';
import ButtonComp from '/common/componentsTpl/ButtonComp';
import ListButton from '/common/componentsTpl/ListButton';
import AutoCompleteComp from '/common/componentsTpl/AutoCompleteComp';
import CalendarComp from '/common/componentsTpl/CalendarComp';
import DisplayLink from '/common/componentsTpl/DisplayLink';
import InputComp from '/common/componentsTpl/InputComp';
import SearchInput from '/common/componentsTpl/SearchInput';
import React from 'react';

const complexComps = {
  BUSINESS_DATA_GRID: BusinessDataGrid, // 索引-组件
  DATA_GRID: DataGrid, // 列表-组件
  STATIC_IMG: StaticImg, // 含图标的说明/提示-组件
  TAB_NAV: TabNav, // 页签 -组件
  DOCUMENT: DocComp, // 文档附件管理-组件
  // 应用特有组件
  ACCT_ITEM_RES_TARGET_CONF_EDITOR: AcctItemResTarEditor, // 科目核算对象配置编辑器-组件
  ACCT_ITEM_RES_ITEM_CONF_EDITOR: AcctItemResItemEditor, // 科目核算项目配置编辑器-组件
};

const simpleComps = {
  FINDER: Finder, // 输入框+搜索+弹框选择-组件
  COMBO_BOX: ComboBox, // 下拉框-组件
  CHECK_BOX: CheckBoxComp, // checkbox-组件
  AUTO_COMPLETE: AutoCompleteComp, // 自动完成 - 组件
  CODER: InputComp, // 编号-组件
  TEXT_INPUT: InputComp, // 文本框-组件
  NUMBER_INPUT: InputComp, // 数字输入-组件
  EXPRESSION: InputComp, // 表达式-组件
  DISPLAY_LINK: DisplayLink, // 链接（只用于展示）-组件
  ADDR_INPUT: SelectArea, // 地区-组件
  CALENDAR: CalendarComp, // 日期-组件
};

const noLabelComps = {
  SEARCH_INPUT: SearchInput,
  BUTTON: ButtonComp, // 按钮
  ACTIVITY_BUTTON: ButtonComp, // 按钮
  LIST_BUTTON: ListButton, // 按钮组-组件
};

export const allComps = assign({}, complexComps, simpleComps, noLabelComps);

export const complexCompNames = keys(complexComps);
export const noLabelCompNames = keys(noLabelComps);

export const renderObjFunc = (obj, cb) => {
  if (!isFunction(cb)) return null;

  if (isArray(obj)) {
    obj.forEach(cb);
  } else if (isObject(obj)) {
    cb(obj, 'obj_1');
  }
};

/**
 *
 * @param item 组件模板数据对象
 * @param iKey
 * @param returnCompClass 是否直接返回组件的Class
 * @returns {string|null|*}
 */
export const renderComponentItem = (item, iKey, returnCompClass = false) => {
  let tmpType = item['_type'];

  if (!isString(tmpType)) return null;

  if (-1 !== tmpType.indexOf('_FILTER')) {
    tmpType = tmpType.substr(0, tmpType.indexOf('_FILTER'));
  }

  let Comp = allComps[tmpType];

  if (Comp) {
    return returnCompClass ? Comp : <Comp key={iKey} compSetting={item}/>;
  } else {
    return `${item['_desc']} ${tmpType} `;
  }
};
